﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using InformationSystem.BLL.Core.Model;
using InformationSystem.BLL.Core.Service;

namespace InformationSystem.WebSite.Module.NoticeMgr
{
    public partial class ContactAjax : System.Web.UI.Page
    {
        public IUserService UserService { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "text/json";

            var start = 0;
            var limit = 10;
            var sort = string.Empty;
            var dir = string.Empty;
            var query = string.Empty;

            if (!string.IsNullOrEmpty(Request["start"]))
            {
                start = int.Parse(Request["start"]);
            }

            if (!string.IsNullOrEmpty(Request["limit"]))
            {
                limit = int.Parse(Request["limit"]);
            }

            if (!string.IsNullOrEmpty(Request["sort"]))
            {
                sort = Request["sort"];
            }

            if (!string.IsNullOrEmpty(Request["dir"]))
            {
                dir = Request["dir"];
            }

            if (!string.IsNullOrEmpty(Request["query"]))
            {
                query = Request["query"];
            }

            List<SearchResult> resultlist = (List<SearchResult>)UserService.GetUserContactWithFilter(query);
            if ((start + limit) > resultlist.Count)
            {
                limit = resultlist.Count - start;
            }
            List<SearchResult> rangePlants = (start < 0 || limit < 0) ? resultlist : resultlist.GetRange(start, limit);
            Paging<SearchResult> people = new Paging<SearchResult>(rangePlants, resultlist.Count);

            Response.Write(string.Format("{{total:{1},'people':{0}}}", JSON.Serialize(people.Data), people.TotalRecords));

        }
    }
}